package com.ctibook.booksystem.dao.bookcatalog.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.cti.ftpservice.dao.impl.BaseDao;
import com.ctibook.booksystem.dao.bookcatalog.BookInfoCategoryDao;
import com.ctibook.booksystem.model.bookcatalog.BookInfoCategory;

@Repository("bookInfoCategoryDao")
public class BookInfoCategoryDaoImpl extends BaseDao implements BookInfoCategoryDao {

	@Override
	public void addOrUpdateBookInfoCategory(BookInfoCategory bookInfoCategory) {
		addOrUpdateObj(bookInfoCategory);
	}

	@Override
	public void deleteBookInfoCategory(BookInfoCategory bookInfoCategory) {
		deleteObj(bookInfoCategory);
	}

	@Override
	public BookInfoCategory getById(String id) {
		return getModel(BookInfoCategory.class, id);
	}

	@Override
	public BookInfoCategory getTopCategpryByName(String category) {
		String hql=" from "+BookInfoCategory.class.getName()+" t where t.category=:category and t.parentId is null";
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("category", category);
		return getSingleObject(hql, maps);
	}
	
	
	public List<BookInfoCategory> getAllTopCategory(){
		String hql=" from "+BookInfoCategory.class.getName()+" t where  t.parentId is null";
		return getListObject(hql);
	}
	
	public BookInfoCategory getChildCategoryByNameAndParent(String category,String parentId){
		String hql=" from "+BookInfoCategory.class.getName()+" t where t.category=:category and t.parentId=:parentId";
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("category", category);
		maps.put("parentId", parentId);
		return getSingleObject(hql, maps);
	}

	@Override
	public List<BookInfoCategory> getChildrenCategoryByParentId(String parentId) {
		String hql=" from "+BookInfoCategory.class.getName()+" t where  t.parentId=:parentId";
		Map<String, Object> maps=new HashMap<String, Object>();
		maps.put("parentId", parentId);
		return getListObject(hql, maps);
	}

}
